package team.se.tms.controller.code;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import team.se.tms.constant.SessionConstant;
import team.se.tms.constant.SystemConstant;
import team.se.tms.controller.BaseController;
import team.se.tms.util.Jurisdiction;
import team.se.tms.util.VerificationCodeUtil;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;

/**
 * 验证码控制层
 *
 * @author winkyle
 * @date 2018/11/12
 */
@Slf4j
@Controller
@RequestMapping(value="/code")
public class VerificationCodeController extends BaseController{

    /**
     * 更换验证码
     *
     * @param response 响应
     * @throws Exception 异常
     * @create on 2018/11/15 by winkyle
     */
    @RequestMapping(value="/verification_code")
    public void changeVerificationCode(HttpServletResponse response) throws Exception {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        Jurisdiction.getSession().setAttribute(SessionConstant.DEPARTMENT, SystemConstant.DEPARTMENT);
        Jurisdiction.getSession().setAttribute(SessionConstant.SYSTEM_NAME, SystemConstant.SYSTEM_NAME);
        Jurisdiction.getSession().setAttribute(SessionConstant.VERIFICATION_CODE, VerificationCodeUtil.drawImg(output));
        ServletOutputStream out = response.getOutputStream();
        output.writeTo(out);
        out.close();
    }
}
